<?php
/*
ENVOI DE SMS VIA LE MODULE CALENDAR DE GOOGLE
EN PHP (via socket)
CREE PAR GRIGIS GAETAN
	cipher16@gmail.com
	http://www.gaetan-grigis.eu
	http://blog.gaetan-grigis.eu
INSPIRE PAR LE MODELE DE MACSIM VIA CURL
http://macsim.labolinux.net/index.php/post/2008/09/26/SmsAlert-2
*/
	include("http.class.php");

	$dt  = new dateTime();
	$dt  ->setTimeZone(new DateTimeZone("Europe/Paris"));
	$dt  ->modify('+ 5 minutes + 30 seconds');
	$now = $dt->format(DateTime::RFC3339);
	$dt  ->modify('+ 15 seconds');
	$now5sec = $dt->format(DateTime::RFC3339);
/*CONFIGURATION PART :*/
	
	//DONNEES D'AUTHENTIFICATION
	$post_data = array();
	$post_data['Email']  = "mon mail@gmail.com";
	$post_data['Passwd'] = "mon pass";
	$post_data['source'] = "exampleCo-exampleApp-1";
	$post_data['service'] = "cl";
	$post_data['accountType'] = "GOOGLE";

	//DONNEES PROPRE AU CONTENU ET A L'HEURE DE RECEPTION DU SMS
	$s = array();
	$s["title"] = "Simple test";
	$s["content"] = "";
	$s["where"] = "Lieu de test";
	$s["startTime"] = $now;	$s["endTime"] = $now5sec;

	$cle_auth = "";
	$SID      = "";
	$LSID     = "";

	$sock = new SockHttp("www.google.com",443,"ssl://");
//RECUPERATION DE LA CLE
	$contenu = $sock->post("/accounts/ClientLogin",$post_data);

	foreach($contenu['BODY'] as $content)
	{
		if(preg_match("#^Auth=(.*)$#i",$content,$auth_key))
			$cle_auth = $auth_key[1];
		if(preg_match("#^SID=(.*)$#i",$content,$auth_key))
			$SID = $auth_key[1];
		if(preg_match("#^LSID=(.*)$#i",$content,$auth_key))
			$LSID = $auth_key[1];
	}
$cle_auth or die("Probleme lors de l'authentification, le login et le mot de passe ne semble pas etre bon");
//CREATION DE L'EVENT

      $_entry = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>
  <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category>
  <title type='text'>".$s["title"]."</title>
  <content type='text'>".$s["content"]."</content>
  <author>
    <name>".$post_data['Email']."</name>
    <email>".$post_data['Email']."</email>
  </author>
  <gd:transparency value='http://schemas.google.com/g/2005#event.opaque'></gd:transparency>
  <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'></gd:eventStatus>
  <gd:where valueString='".$s["where"]."'></gd:where>
  <gd:when startTime='".$s["startTime"]."' endTime='".$s["endTime"]."'>
    <gd:reminder minutes='5' />
  </gd:when>
</entry>";
      $header[] = "MIME-Version: 1.0";
      $header[] = "Accept: text/xml";
      $header[] = "Authorization: GoogleLogin auth=".$cle_auth;
      $header[] = "Cache-Control: no-cache";
      $header[] = "Cookie: SID=".$SID;//."; LSID=".$LSID;

//EXECUTION DE L'EVENT
$retour = $sock->post("/calendar/feeds/default/private/full",array(),$_entry,$header,"application/atom+xml");
/*POUR DEBUG
foreach($retour['HEAD'] as $content)
	echo $content."<br />";
echo "<br />------<br />";
foreach($retour['BODY'] as $content)
	echo $content."<br />";*/
echo "Envoi ok (normalement ... si vous avez renseigne le champs du telephone portable dans google calendar)";
?>
